package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsKeyword;
import com.qf.service.DtsAdminCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class DtsAdminCouponServiceImpl implements DtsAdminCouponService {

    @Autowired
    DtsCouponMapper couponMapper;

    @Override
    public IPage<DtsCoupon> queryPage(Integer page, Integer limit, String sort, String order, String name, Integer type, Integer status) {
        IPage<DtsCoupon> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCoupon> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            qw.like("`name`",name);
        }
        if(type != null){
            qw.eq("`type`",type);
        }
        if (status != null){
            qw.eq("status",status);
        }
        qw.eq("deleted",false);
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        couponMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public DtsCoupon queryById(Integer id) {
        LambdaQueryWrapper<DtsCoupon> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsCoupon::getId,id);
        DtsCoupon coupon = couponMapper.selectOne(qw);
        return coupon;
    }

    @Override
    public DtsCoupon insert(DtsCoupon coupon) {
        coupon.setAddTime(new Date());
        coupon.setUpdateTime(new Date());
        coupon.setDeleted(false);
        couponMapper.insert(coupon);
        return coupon;
    }

    @Override
    public void update(DtsCoupon coupon) {
        coupon.setDeleted(false);
        coupon.setUpdateTime(new Date());
        couponMapper.updateById(coupon);
    }

    @Override
    public void delete(DtsCoupon coupon) {
        coupon.setUpdateTime(new Date());
        coupon.setDeleted(true);
        couponMapper.updateById(coupon);
    }
}
